package com.attendance.service;

import java.util.ArrayList;
import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.attendance.dao.AbsenceRepository;
import com.attendance.entity.Absence;
import com.attendance.entity.dto.AbsenceDTO;
import com.staff.entity.Staff;

@Service
@Transactional //缺勤表
public class AbsenceService implements IAbsenceService{
	@Autowired
	private AbsenceRepository absenceRepository;
	
	public void saveOrUpdate(Absence absence) {
		// TODO Auto-generated method stub
		absenceRepository.save(absence);
	}

	
	public void delete(Long id) {
		// TODO Auto-generated method stub
		Absence absence=absenceRepository.findOne(id);
		if(absence!=null) {
			absence.setStaff(null);
			absenceRepository.save(absence);
			absenceRepository.delete(absence);
		}		
	}

	
	public void delete(List<Long> ids) {
		// TODO Auto-generated method stub
		List<Absence> absenceList=(List<Absence>) absenceRepository.findAll(ids);
		if(absenceList!=null) {
			for(Absence absence :absenceList) {
				absence.setStaff(null);
				absenceRepository.save(absence);
			}
			absenceRepository.delete(absenceList);
		}

	}

	@Transactional(readOnly=true)
	public Absence findOne(Long id) {
		// TODO Auto-generated method stub
		return absenceRepository.findOne(id);
	}

	@Transactional(readOnly=true)
	public List<Absence> findAll() {
		// TODO Auto-generated method stub
		return (List<Absence>) absenceRepository.findAll();
	}

	@Transactional(readOnly=true)
	public Page<Absence> findAll(Specification<Absence> spec, Pageable pageable) {
		// TODO Auto-generated method stub
		return absenceRepository.findAll(spec, pageable);
	}


	public Page<AbsenceDTO> findPage(String staffName, String staffNum, Pageable pageable) {
		List<AbsenceDTO> results = new ArrayList<AbsenceDTO>();
		List<Absence> list = new ArrayList<Absence>();
		if(staffName!=""&&staffName!=null) {
			list = absenceRepository.findLikeStaffName(staffName);
		}else if(staffNum!=""&&staffNum!=null) {
			list = absenceRepository.findLikeStaffNum(staffNum);
		}else {
			list = (List<Absence>) absenceRepository.findAll();
		}
		for(Absence a:list) {
			Staff staff = a.getStaff();
			if(staff == null)return null;
			String dept = null;
			if(staff!=null) {
				if(staff.getDepartment()!=null)dept = staff.getDepartment().getName();
			}
			results.add(new AbsenceDTO(staff, a, dept));
		}
		List<AbsenceDTO> page = new ArrayList<AbsenceDTO>();
		int i = pageable.getPageNumber()*pageable.getPageSize();
		int n = i+pageable.getPageSize();
		while(i<results.size()&&i<n) {
			page.add(results.get(i));
			i++;
		}
		return new PageImpl<AbsenceDTO>(page,pageable,page.size());
	}
}
